package com.google.code.mamule.config;

import com.google.code.mamule.delivery.MailDelivery;
import com.google.code.mamule.delivery.MockDelivery;
import com.google.code.mamule.integration.PricingStrategyHolder;
import com.google.code.mamule.mail.MailRepository;
import com.google.code.mamule.order.OrderRepository;
import com.google.code.mamule.price.PriceRepository;
import com.google.code.mamule.price.PricingStrategy;
import com.google.code.mamule.print.MailPrinter;
import com.google.code.mamule.print.MockPrinter;
import com.google.code.mamule.process.MailRenderer;
import com.google.code.mamule.process.MockRenderer;
import com.google.code.mamule.user.User;
import com.google.code.mamule.user.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA. User: borck_000 ; Date: 12/21/13 ; Time: 8:46 PM
 */
@Configuration
@ComponentScan("com.google.code.mamule")
public class MamuleContainer {

  /* spring magic */

  // populate work around pricing strategy holder
  @Autowired
  public void populateStrategyHolder(PricingStrategy strategy) {
    PricingStrategyHolder.set(strategy);
  }

  // save admin user
  @Autowired
  public void createAdminAccount(UserRepository users) {
    User admin = users.findOne("admin");
    if (admin == null) {
      admin = new User();
      admin.setUsername("admin");
      admin.setPassword("root");
      users.save(admin);
    }
  }

  // mock 3rd party services

  @Bean
  public MailPrinter mailPrinter() {
    return new MockPrinter();
  }

  @Bean
  public MailDelivery mailDelivery() {
    return new MockDelivery();
  }

  @Bean
  public MailRenderer mailRenderer() {
    return new MockRenderer();
  }

  // init mock entities

  @Autowired
  @Transactional
  public void injectMocks(UserRepository users, MailRepository mails, OrderRepository orders,
                          PriceRepository prices) {
    /*
  User current = new User("chris", "borckholder", "chris@mamule.com", "11111", null);
    users.save(current);
    current = new User("daniel", "tieber", "daniel@mamule.com", "22222", null);
    users.save(current);
    current = new User("benedikt", "pittl", "benedikt@mamule.com", "33333", null);
    users.save(current);
    // mails
    User owner = users.findOne("benedikt");
    Mail mail = new Mail(owner, null, "first mail text", MailStatus.TRANSIENT,
                         Collections.<MailOption>emptySet());
    mails.save(mail);
    mail = new Mail(owner, null, "second mail text", MailStatus.TRANSIENT,
                    Collections.<MailOption>emptySet());
    mail = mails.save(mail);
    // order
    Order order = new Order();
    order.setItems(Collections.singleton(mail));
    order.setOwner(owner);
    orders.save(order);
    // price
    Price p=new Price();
    p.setPrice(new BigDecimal(5));
    prices.save(p);*/
  }
}
